﻿#region FileHeader
//////////////////////////////////////////////////////////////////////////////////
// Author:                          Hector Llanos
// Date:                            08.27.2010
// FileName:                        CMessageSystem.cs
// Reason:                          manager to determine how they will be sent
//////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MessagingSystem
{
    public sealed class CMessageSystem
    {
        public delegate void MESSAGEPROC( CBaseMessage baseObject);
        private static CMessageSystem m_pInstance = new CMessageSystem();

        private Queue<CBaseMessage> m_qMsgQueue;
        MESSAGEPROC m_pfnMsgProc;

        public static CMessageSystem Instance
        {
            get { return m_pInstance; }
        }

        private CMessageSystem()
        {
            m_qMsgQueue = null;
            m_pfnMsgProc = null;
        }
        ~CMessageSystem()
        {

        }

        //	How many messages waiting to be processed.
        public int GetNumMessages()
        {
            return (int)m_qMsgQueue.Count;
        }

        //	Setup the function pointer for our messages.
        public void InitMessageSystem(MESSAGEPROC pfnMsgProc)
        {
            //make sure the message proc is valid
            if (pfnMsgProc == null)
                return;

            // init the queue
            m_qMsgQueue = new Queue<CBaseMessage>();

            // get the msg proc
            m_pfnMsgProc = pfnMsgProc;
        }

        //	Sends the message into the queue and awaits processing later on through
        //	The ProcessMessages() function.
        public void SendMessage(CBaseMessage pMsg)
        {
            //make sure the message exists
            if (pMsg == null)
                return;
            // send the message to the queue for processing later on
            m_qMsgQueue.Enqueue(pMsg);
        }

        //	Processes all the messages that are waiting inside of the queue.
        //	Normally you only call this function once per frame.
        public void ProcessMessages( )
        {
            //error check to make sure we get a message proc
            if (m_pfnMsgProc == null)
                return;

            //go through the entire queue and process the messages that are waiting
            while (m_qMsgQueue.Count > 0)
            {
                //process the first message
                m_pfnMsgProc(m_qMsgQueue.Peek());

                //go to the next message
                m_qMsgQueue.Dequeue();
            }
        }

        //	Clears any messages that may be remaining
        public void ClearMessages(  )
        {
            m_qMsgQueue.Clear();
        }

        //	Clears any messages that may be remaining and sets the function pointer to NULL.
        public void ShutdownMessageSystem(  )
        {
            ClearMessages();
            m_pfnMsgProc = null;
        }

    }
}
